package fr.enssat.embeddedsoft.projet.sumo.abouzidi.mndiaye;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.Toast;

public class AccSonsor extends Activity implements SensorEventListener{
	
	/** variables *************************************************************/
	private SensorManager mSensorManager;
	private Sensor mAccSensor;
	float accX, accY, accZ;
	
	/** constructor ***********************************************************/
	public AccSonsor(){
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (mAccSensor == null){
        	Toast.makeText(this, R.string.no_acc_sonsor, Toast.LENGTH_LONG).show();
        	finish();
        	return;
        }
        // we have an accelerometer on the device :) !
        register();
        
        accX = 0;
        accY = 0;
        accZ = 0;
	}
	
	/** getters ***************************************************************/
	float getX(){ return accX;}
	float getY(){ return accY;}
	float getZ(){ return accZ;}
	
	/** methods ***************************************************************/
	void register(){
		mSensorManager.registerListener(this, mAccSensor, SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	void unregister(){
		if (mSensorManager != null) {
			mSensorManager.unregisterListener(this);
		}
	}
	
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
	}

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		
		// if it is an accelerometer
		if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

			accX = event.values[0];
			accY = event.values[1];
			accZ = event.values[2];
		}
	}
	
	public String display(){
		return   "X : " + accX + 
			   "\nY : " + accY +
			   "\nZ : " + accZ;
	}
	// end !
}
